<?php

namespace app\admin\controller;

use app\admin\model\Paylog;
use app\Request;
use think\facade\Db;

class Order extends Authorization
{
    public function getPaylogList(Request $request){
        $params = $request->param();
        $where = [];
        $field = 'p.id,p.commodity_id,p.users_id,p.value,p.status,p.create_time,u.nickname,u.team,c.name as commodity,c.type as commodity_type';
        if(isset($params['status']) && !empty($params['status'])){
            $where[] = ['p.status','=',$params['status']];
        }
        if(isset($params['nickname']) && !empty($params['nickname'])){
            $where[] = ['u.nickname','like','%'.$params['nickname']."%"];
        }
        if(isset($params['team']) && !empty($params['team'])){
            $where[] = ['u.team','=',$params['team']];
        }
        if(isset($params['type']) && !empty($params['type'])){
            $where[] = ['c.type','=',$params['type']];
        }
        $data = count($where) > 0 ? Db::table('paylog')->alias('p')->join('users u','p.users_id=u.id')->join('commodity c','p.commodity_id=c.id')->where($where)->page($params['pageNum'],$params['pageSize'])->field($field)->order('status desc,id desc')->select() :
            Db::table('paylog')->alias('p')->join('users u','p.users_id=u.id')->join('commodity c','p.commodity_id=c.id')->field($field)->order('status desc,id desc')->select();
        $count = count($where) > 0 ? Db::table('paylog')->alias('p')->join('users u','p.users_id=u.id')->join('commodity c','p.commodity_id=c.id')->where($where)->count() :
            Db::table('paylog')->alias('p')->join('users u','p.users_id=u.id')->join('commodity c','p.commodity_id=c.id')->count();

        return $this->success('ok',['list'=>$data,'total'=>$count,'params'=>$params]);
    }

    public function edit(Request $request){
        $params = $request->param();
        $data = [];
        if($params['status'] == '已处理'){
            $data['status'] = 1;
        }else {
            $data['status'] = 2;
        }
        $sql = (new Paylog())->where('id',$params['id'])->update($data);
        if (!$sql) return $this->error('更新失败，未作任何更改');
        return $this->success('ojbk',$params);
    }
}